package com.teamcalendar.api;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.teamcalendar.client.rest.RequestMethod;
import com.teamcalendar.client.rest.RestClient;

public class CreateJson {
	String jsonObjectType;
	String response;
	String mKey;
	String firstName;
	String lastName;
	String email;
	String userName;
	String password;
	String json = "{}";
	String newJson = null;
	public static boolean LoggedIn=false;
	
	String memberId;
	String address;
	String phonenumber;
	String location;
	String zipCode;
	String teamId;
	String eventId;
	String eventDetails;
	String date;
	List<String> recipientIds=new ArrayList<String>();

	String message;
	ArrayList<String> recipientId;
	String senderId;
	String messageId;
	static final String serverAddress="http://www.google.sportteamcalendar.appspot.com/";
	public static final String appAddress="ListenForRestClientServlet.do";
	public static final String imagesFolder="PICS/";
	public String createJsonObject(){
		try {
			JSONObject jsonObject = new JSONObject(json);
			JSONObject teamObject = new JSONObject();
			JSONObject newOrder = new JSONObject();
			teamObject.put("order", newOrder);
			jsonObject.put("orders", teamObject);
			newOrder.put("command", jsonObjectType);
			newOrder.put("key", mKey);
			newOrder.put("firstname", firstName);
			newOrder.put("lastname", lastName);
			newOrder.put("username", userName);
			newOrder.put("address", address);
			newOrder.put("phonenumber", phonenumber);
			newOrder.put("location", location);
			newOrder.put("zipcode", zipCode);
			newOrder.put("memberid", memberId);
			newOrder.put("password", password);
			newOrder.put("email", email);
			newOrder.put("teamid", teamId);
			newOrder.put("activityid", eventId);
			newOrder.put("date", date);
			newOrder.put("message", message);
			newOrder.put("messageid", messageId);
			newOrder.put("recipientids", recipientId);
			newOrder.put("senderid", senderId);
			newOrder.put("activity",eventDetails);
			if(jsonObjectType.contains("sendmessage")){
				createMesseges(newOrder);
			}
			newJson = jsonObject.toString();

		}catch(JSONException e) {
			newJson = null;
		}
		try
		{
			RestClient client = new RestClient(serverAddress+appAddress);
			client.AddParam("message", newJson);
			client.Execute(RequestMethod.GET);
			response = client.getResponse();
			//fromJSONTeam(response);
			if(jsonObjectType=="login"){
				LoggedIn=true;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return newJson;
	}
	
	private void createMesseges(JSONObject json){

		int index=1;
			try {			
				json.put("noofrecipients", recipientIds.size()); 
				for (int i = 0; i < recipientIds.size(); i++) {
					json.put("id" + String.valueOf(index),recipientIds.get(i));
					index++;
				}
				recipientIds.clear();
	
			}catch(JSONException e) {
				newJson = null;
			}
	}
	
	

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getJsonObjectType() {
		return jsonObjectType;
	}

	public void setJsonObjectType(String jsonObjectType) {
		this.jsonObjectType = jsonObjectType;
	}

	public String getmKey() {
		return mKey;
	}

	public void setmKey(String mKey) {
		this.mKey = mKey;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public String getNewJson() {
		return newJson;
	}

	public void setNewJson(String newJson) {
		this.newJson = newJson;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	public String getResponse() {
		return response;
	}
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getMemberId() {
		return memberId;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhonenumber() {
		return phonenumber;
	}

	public void setPhonenumber(String phonenumber) {
		this.phonenumber = phonenumber;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
		public String getTeamId() {
		return teamId;
	}

	public void setTeamId(String teamId) {
		this.teamId = teamId;
	}

	public String getEventId() {
		return eventId;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}


	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public ArrayList<String> getRecipientId() {
		return recipientId;
	}

	public void setRecipientId(ArrayList<String> recipientId) {
		this.recipientId = recipientId;
	}

	public String getSenderId() {
		return senderId;
	}

	public void setSenderId(String senderId) {
		this.senderId = senderId;
	}

	public String getEventDetails() {
		return eventDetails;
	}

	public void setEventDetails(String eventDetails) {
		this.eventDetails = eventDetails;
	}

	

	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	public List<String> getRecipientIds() {
		return recipientIds;
	}

	public void setRecipientIds(List<String> recipientIds) {
		this.recipientIds = recipientIds;
	}
		
}
